package com.yewen.http.repository;

import com.yewen.http.entity.Account;
import com.yewen.http.entity.LoginAccount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

/**
 * @Classname AccountRepository
 * @Description TODO
 * @Date 2022/3/30 22:17
 * @Created by TaoVh
 */
@Repository
public class AccountRepository {
    @Autowired
    private MongoTemplate mongoTemplate;

    public boolean save(Account account){
        if (account==null)return false;
        mongoTemplate.save(account,"account");
        return true;
    }
    public Account findAccountById(String id){
        Query query = new Query(Criteria.where("id").is(id));
        return mongoTemplate.findOne(query,Account.class);
    }
    public Account findAccountByTel(String tel){
        Query query = new Query(Criteria.where("tel").is(tel));
        return mongoTemplate.findOne(query,Account.class);
    }
    public Account findAccountByEmail(String email){
        Query query = new Query(Criteria.where("email").is(email));
        return mongoTemplate.findOne(query,Account.class);
    }
    public boolean loginAuth(LoginAccount loginAccount){
        Query query = new Query(Criteria.
                where("id").is(loginAccount.getId()).
                and("password").is(loginAccount.getPassword()));
        return mongoTemplate.findOne(query, Account.class) != null;
    }
}
